package com.mywork.test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
import java.util.Set;

/**
 * @ProjectName: linux给文件夹授权  只支持 linux系统  window报错！
 * @ClassName: com.mywork.test
 * @Author: lsx
 * @Date: 2023/03/16 10:15
 **/
public class FileUtil {

    public static void should_create_folder_with_permission() throws IOException {

        Set<PosixFilePermission> fullPermission = new HashSet<>();
        fullPermission.add(PosixFilePermission.OWNER_EXECUTE);
        fullPermission.add(PosixFilePermission.OWNER_READ);
        fullPermission.add(PosixFilePermission.OWNER_WRITE);

        fullPermission.add(PosixFilePermission.GROUP_EXECUTE);
        fullPermission.add(PosixFilePermission.GROUP_READ);
        fullPermission.add(PosixFilePermission.GROUP_WRITE);

        fullPermission.add(PosixFilePermission.OTHERS_EXECUTE);
        fullPermission.add(PosixFilePermission.OTHERS_READ);
        fullPermission.add(PosixFilePermission.OTHERS_WRITE);

        java.nio.file.Path path = Paths.get("/usr/local/filetest");
        try {
            Files.setPosixFilePermissions(path, fullPermission);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void outputLS(java.nio.file.Path path) throws IOException {
        System.out.println(new java.util.Scanner(
                Runtime.getRuntime().exec("ls -alt " + path.toAbsolutePath() + "/..")
                        .getInputStream()).useDelimiter("\\A").next());
    }

    public static void main(String[] args){
        try {
            should_create_folder_with_permission();
        } catch (IOException e) {

        }
    }


}
